﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL.DHelpers;
using System.Security.Cryptography;
using System.Text;
using DAL;
using System.Data;
using DAL.DCollector;

namespace SaleOnlineSystem.Controls
{
    public partial class BoxRegister : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                captchaImage.ImageUrl = new CaptchaProvider().CreateCaptcha();
                txtLoginUser.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
                txtLoginPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
                txtUsername.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtConPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtEmail1.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtPhone.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtAddress.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");


            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Bank", "Bank_ID"));
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string fullname = txtName.Text;
            string email = txtEmail1.Text;
            string phonenumber = txtPhone.Text;
            string Address = txtAddress.Text;
            string password_mahoa = LIB.Converter.Encode("sha1", password);
            CaptchaProvider captchaPro = new CaptchaProvider();
            DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.StoredProcedureForgetPassword("checkemployeeexit", username, email);
            if (checkuseremail.Rows[0]["result"].ToString() == "0")
            {
                if (captchaPro.IsValidCode(txtCaptcha.Text))
                {
                    if (nextid == 0)
                    {
                        string value = "1,N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + Address + "','/Images/Image-User/default.jpg','1'";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Employee", "insertemployee", values, false);
                        Session["Username"] = txtUsername.Text;
                        Session["Employee_FullName"] = fullname;
                        Response.Redirect("~/thanhcong.htm");
                    }
                    else
                    {
                        int idnextemployee = nextid + 1;
                        string value = "" + idnextemployee + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + phonenumber + "',N'" + Address + "','/Images/Image-User/default.jpg','1'";
                        List<string> values = new List<string>();
                        values.Add(value);
                        DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_Employee", "insertemployee", values, false);
                        Session["Username"] = txtUsername.Text;
                        Session["Employee_FullName"] = fullname;
                        Response.Redirect("~/thanhcong.htm");
                    }

                }
                else
                {
                    lblCheckCaptcha.Text = "Mã bảo mật không đúng";
                }
            }
            else
            {
                lblCheckUserAndEmail.Text = "Tên tài khoản hoặc Email đã tồn tại";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string loginuser = txtLoginUser.Text;
            string loginpass = txtLoginPassword.Text;
            string matkhaulogin = LIB.Converter.Encode("sha1", loginpass);
            DataTable login = DAL.DCollector.DBCollector.Instance.StoredProcedureLogin("login", loginuser, matkhaulogin);

            if (login.Rows.Count == 0)
            {
                Response.Redirect(Request.Url.ToString());
            }
            else
            {
                if (Session["Username"] == null)
                {
                    Session["Employee_ID"] = login.Rows[0]["Employee_ID"].ToString();
                    Session["Employee_Email"] = login.Rows[0]["Employee_Email"].ToString();
                    Session["Employee_Avatar"] = login.Rows[0]["Employee_Avatar"].ToString();
                    Session["Employee_FullName"] = login.Rows[0]["Employee_FullName"].ToString();
                    Session["Username"] = txtLoginUser.Text;
                    Session["Password"] = txtLoginPassword.Text;

                    HttpCookie cki = new HttpCookie("CookieUser");
                    cki.Value = txtLoginUser.Text;
                    Response.Cookies.Add(cki);
                    cki.Expires = DateTime.MaxValue;
                    Response.Redirect("~/trangchu.htm");
                }
            }
        }
    }
}